package com.txw.javaCore.server;

import com.txw.javaCore.api.enums.DataDictEnum;
import com.txw.javaCore.model.mapper.DataDictMapper;
import com.txw.javaCore.server.common.SystemDataConfig;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

import javax.annotation.PostConstruct;

/**
 * @PROJECT_NAME: JavaCoreB2
 * @DESCRIPTION:
 * @USER: 涂玄武
 * @DATE: 2020/12/17 10:17
 */
@SpringBootApplication
@ImportResource(value = {"classpath:spring/spring-jdbc.xml","classpath:spring-job.xml"})
@MapperScan(basePackages = "com.txw.javaCore.model.mapper")
@EnableScheduling
@EnableAsync
@Configuration
@Slf4j
public class MainApplication extends SpringBootServletInitializer {

//    数据预加载的两种方式：
//      1、@postConstruct：项目启动过程中完成预加载，若数据库数据发生变化，只能重启更新本地缓存的数据，使用场景-针对于当前类文件
//      2、CommandLineRunner接口：项目启动完成后预加载，若数据有更新仅需再次调用初始化数据的方法，使用场景-服务于整个项目,可用@Order注解排列数据加载的顺序
//
//    @Autowired
//    private DataDictMapper dictMapper;
//
//    @PostConstruct
//    public void initAppDataDict(){
//        log.info("--系统数据初始化服务开始--");
//        SystemDataConfig.putAppDataDictOs(dictMapper.selectByType(DataDictEnum.AppOsType.getCode()));
//        SystemDataConfig.putAppDataDictDevice(dictMapper.selectByType(DataDictEnum.AppDeviceType.getCode()));
//    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(MainApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }
}
